package com.quotemanager.ui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.nirvana.core.Validator;
import com.quotemanager.businessbean.BrandBean;
import com.quotemanager.exception.DataAccessException;

public class Brand implements ActionListener {

	JFrame brandFrame = null;
	JLabel brandLabel = null;
	JTextField brandField = null;
	JButton addButton = null;
	JButton cancelButton = null;
	JPanel dataPanel = null;
	JPanel buttonPanel = null;
	JPanel mainPanel =  null;
	
	/**
	 * @param args
	 */
	Brand(){
		brandLabel =  new JLabel("Brand ");
		brandField =  new JTextField(10);
		
		dataPanel = new JPanel(new GridLayout(1,2));
		dataPanel.add(brandLabel);
		dataPanel.add(brandField);
		
		addButton = new JButton("Add");
		cancelButton = new JButton("Cancel");
		addButton.addActionListener(this);
		cancelButton.addActionListener(this);
		buttonPanel = new JPanel(new GridLayout(1,2));
		buttonPanel.add(addButton);
		buttonPanel.add(cancelButton);
		
		mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
		mainPanel.add(dataPanel);
		mainPanel.add(buttonPanel);
		
		
		brandFrame = new JFrame("Brand");
		brandFrame.getContentPane().add(mainPanel);
		brandFrame.setSize(400, 200);
		brandFrame.pack();
		brandFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		brandFrame.setVisible(true);
		
	}
	
	public static void main(String[] args) {
		new Brand();
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == addButton){
			String brandName = brandField.getText();
			System.out.println(brandName+"::");
			if(!Validator.isNullOrEmpty(brandName)){
				BrandBean brandRegistration = new BrandBean();
				boolean insertStatus = false;
				try {
					insertStatus = brandRegistration.registerBrand(brandName);
				} catch (DataAccessException e1) {
					e1.printStackTrace();
				}
				if(insertStatus)
					JOptionPane.showMessageDialog(brandFrame, "Brand Registered Successfully");
				else
					JOptionPane.showMessageDialog(brandFrame, "Error in Brand Registration");
			}
			else{
				JOptionPane.showMessageDialog(brandFrame, "Brand Name cannot be empty.");
			}
		}
		if(e.getSource() == cancelButton){
			brandField.setText("");
		}
	}

}
